package com.ytjj.qmyx.admin.service.imService.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.mapper.ImCustomerLabelMapper;
import com.ytjj.qmyx.admin.model.ImCustomerLabel;
import com.ytjj.qmyx.admin.model.dto.ImCustomerLabelDto;
import com.ytjj.qmyx.admin.model.vo.ImCustomerLabelVO;
import com.ytjj.qmyx.admin.service.imService.ImCustomerLabelService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ImCustomerLabelServiceImpl implements ImCustomerLabelService {

    @Resource
    private ImCustomerLabelMapper imCustomerLabelMapper;

    /**
     * 新增标签
     * @param imCustomerLabel
     * @return
     */
    @Override
    public CommonResult insertLabel(ImCustomerLabel imCustomerLabel) {
        ImCustomerLabel imCustomerLabel1 = selectByLabelName(imCustomerLabel.getLabelName());
        if (null != imCustomerLabel1){
            throw new ApiException("该标签名已存在");
        }
        imCustomerLabel.setCreateTime(new Date());
        int count = imCustomerLabelMapper.insertSelective(imCustomerLabel);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    public ImCustomerLabel selectByLabelName(String name) {
        ImCustomerLabel imCustomerLabel = imCustomerLabelMapper.selectByName(name);
        if (null != imCustomerLabel){
            return imCustomerLabel;
        }
        return null;
    }
    /**
     * 删除标签
     * @param imCustomerLabelVO
     * @return
     */
    @Override
    public CommonResult deleteLabel(ImCustomerLabelVO imCustomerLabelVO) {
        int count = imCustomerLabelMapper.deleteByPrimaryKey(imCustomerLabelVO.getId());
        if (count > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改标签
     * @param imCustomerLabelVO
     * @return
     */
    @Override
    public CommonResult updateLabel(ImCustomerLabelVO imCustomerLabelVO) {
        ImCustomerLabel imCustomerLabel = selectByLabelName(imCustomerLabelVO.getLabelName());
        int count = 0;
        if (null != imCustomerLabel && imCustomerLabel.getId() != imCustomerLabelVO.getId()){
            throw new ApiException("该标签名已存在,不能修改为"+imCustomerLabelVO.getLabelName());
        }else {
            count = imCustomerLabelMapper.updateLabel(imCustomerLabelVO.getId(),imCustomerLabelVO.getLabelName(),imCustomerLabelVO.getLabelColour());
        }
        if (count > 0) {
            return CommonResult.success(count);
        }
        return null;
    }

    /**
     * 增加标签使用次数
     * @param userLabel
     * @return
     */
    @Override
    public CommonResult updateLabelUseNum(Byte userLabel) {
        int count = imCustomerLabelMapper.upLabelUseNum(userLabel);
        if (count > 0) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询所有标签
     * @param
     * @return
     */
    @Override
    public CommonPage<ImCustomerLabelDto> selectLabel(ImCustomerLabelVO imCustomerLabelVO) {
        PageHelper.startPage(imCustomerLabelVO.getPageNum(), imCustomerLabelVO.getPageSize());
        List<ImCustomerLabelDto> pageList = imCustomerLabelMapper.selectList();
        return CommonPage.restPage(pageList);
    }

    //客服修改客户信息时 展示的标签
    @Override
    public List<ImCustomerLabelDto> queryLabel() {
        List<ImCustomerLabelDto> list = imCustomerLabelMapper.queryLabel();
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        return list;
    }

    /**
     *  根据id查询标签
     * @param userLabel
     * @return
     */
    @Override
    public ImCustomerLabel selectOne(Byte userLabel) {
        ImCustomerLabel imCustomerLabel = imCustomerLabelMapper.selectOne(userLabel);
        return imCustomerLabel;
    }

    /**
     * 减少标签使用次数
     * @param userLabel
     * @return
     */
    @Override
    public int downLabelUseNum(Byte userLabel) {
        int count = imCustomerLabelMapper.downLabelUseNum(userLabel);
        return count;
    }
}
